import {
  initArg, isYearReport, getPeriod, $ajax
} from './saas.utils.js';

// saas跳转税局
function saasTosj() {
  $('.old-tax').remove();
  let $handleTax = $('.js-handle-tax');
  let $handleTaxOuter = $('.js-handle-tax-outer');
  let html = '<a href="javascript:;" class="js-handle-tax">' + $handleTax.html() + '</a>';
  $handleTaxOuter.html(html);

  $('body').on('click', '.js-handle-tax', function () {
    $ajax('/api/index/queryTax', {}).then(res => {
      if (res.success) {
        let companyInfo = window.renderData.accBook;
        let curPeriodStatus = res.curPeriodStatus; // 会计期间的状态(0-未结账 2-已结账 3-已结帐且已报税)
        // 未结账不打开
        let canTaxObj = { canTax: true };
        if (curPeriodStatus === 0) {
          let currStatus = $('.status-step').find('.curr');
          if (currStatus.length > 0) {
            let errMsg = '当前期间未结账，请先结账';
            alert(errMsg);
            return;
          }
          canTaxObj = { canTax: false };
        } else if (curPeriodStatus === 3) {
          canTaxObj = { canTax: false };
        } else {
          canTaxObj = { canTax: true };
        }
        companyInfo = Object.assign({}, companyInfo, res.data, canTaxObj);
        companyInfo.curYear = window.renderData.year;
        companyInfo.curPeriod = window.renderData.period;
        companyInfo.pagePath = window.location.href;
        companyInfo.useOneClickNum = 0; // 点击次数
        window.postMessage(
          {
            event: 'setBaseInfo',
            data: companyInfo
          },
          '*'
        );
      } else {
        alert('获取报税基本信息失败，请稍候再试！');
      }
    });
  });

}

(async () => {
  saasTosj();
})()


